package com.gdut.simple.view.activity.explore;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.gdut.simple.R;
import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.ToastUtils;
import com.gdut.simple.contract.FollowContract;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.presenter.FollowPresenter;
import com.gdut.simple.view.activity.user.LoginActivity;
import com.gdut.simple.view.adapter.SimplePageAdapter;
import com.gdut.simple.view.basic.AbsBasicActivity;
import com.gdut.simple.view.fragment.UserInfoFragment;
import com.gdut.simple.view.fragment.UserOtherFragment;
import com.gdut.simple.view.fragment.UserPostFragment;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class OtherUserInfoActivity extends AbsBasicActivity implements FollowContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.id_follow_user_profile)
    ImageView mProfileView;
    @BindView(R.id.id_follow_button)
    FloatingActionButton mSubscribeButton;
    @BindView(R.id.id_tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.id_wrap_view_pager)
    ViewPager mPager;

    @OnClick(R.id.id_follow_button)
    void follow() {
        mFollowPresenter.follow();
    }

    private String mUserId;
    private User mUser;

    private FollowContract.Presenter mFollowPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.hide();
            }
        }
        setContentView(R.layout.activity_other_user_info);
        ButterKnife.bind(this);
        new FollowPresenter(this);
    }

    private void initToolbar(User user) {
        if (user != null) {
            toolbar.setTitle(user.getNickName());
            Picasso.with(this)
                    .load(user.getProfile().getUrl())
                    .into(mProfileView);
        }
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    @Override
    protected void loadData() {
        mUserId = getIntent().getStringExtra("objectId");
        mFollowPresenter.getUser();
    }

    private void initTab(User user) {
        final List<String> TABS = new ArrayList<>();
        TABS.add("简介");
        TABS.add("帖子");
        TABS.add("其它");
        final List<Fragment> FRAGMENTS = new ArrayList<>();
        FRAGMENTS.add(UserInfoFragment.newInstance(user));
        FRAGMENTS.add(UserPostFragment.newInstance(user));
        FRAGMENTS.add(UserOtherFragment.newInstance(user));
        SimplePageAdapter mPagerAdapter = new SimplePageAdapter(getSupportFragmentManager(), FRAGMENTS, TABS);
        mPager.setAdapter(mPagerAdapter);
        mPager.setOffscreenPageLimit(TABS.size() - 1);
        tabLayout.setupWithViewPager(mPager);
    }

    @Override
    public void setPresenter(@NonNull FollowContract.Presenter presenter) {
        mFollowPresenter = presenter;
    }

    @Override
    public String getUserId() {
        return mUserId;
    }

    @Override
    public User getUser() {
        return BmobUser.getCurrentUser(User.class);
    }

    @Override
    public User getFollow() {
        return mUser;
    }

    @Override
    public void showFollowButton() {
        mSubscribeButton.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideFollowButton() {
        mSubscribeButton.setVisibility(View.GONE);
    }

    @Override
    public void showLoginBar() {
        Snackbar.make(mPager, "尚未登陆", Snackbar.LENGTH_LONG).setAction("登陆", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(OtherUserInfoActivity.this, LoginActivity.class), 100);
            }
        }).show();
    }

    @Override
    public void setUser(User user) {
        mUser = user;
    }

    @Override
    public void refresh(User user) {
        initToolbar(user);
        initTab(user);
        mFollowPresenter.checkStatus();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            mFollowPresenter.follow();
        }
    }

    public static void startActivity(Activity activity, String userId) {
        Intent intent = new Intent(activity, OtherUserInfoActivity.class);
        intent.putExtra("objectId", userId);
        activity.startActivity(intent);
    }
}
